package com.nd.mobile.study.common.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

/**
 * 日期工具类
 * 
 */
public class DateUtil {

	/**
	 * 获取现在时间
	 * 
	 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 */
	public static Date getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(8);
		Date currentTime_2 = formatter.parse(dateString, pos);
		return currentTime_2;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回字符串格式 yyyy-MM-dd HH:mm
	 */
	public static String getStringDateWithoutSecond() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回短时间字符串格式yyyy-MM-dd
	 */
	public static String getStringDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取时间 小时:分;秒 HH:mm:ss
	 * 
	 * @return
	 */
	public static String getTimeShort() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		Date currentTime = new Date();
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrLong(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrLongNoSecond(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStr(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 得到现在时间
	 * 
	 * @return
	 */
	public static Date getNow() {
		Date currentTime = new Date();
		return currentTime;
	}

	/**
	 * 提取一个月中的最后一天
	 * 
	 * @param day
	 * @return
	 */
	public static Date getLastDate(long day) {
		Date date = new Date();
		long date_3_hm = date.getTime() - 3600000 * 34 * day;
		Date date_3_hm_date = new Date(date_3_hm);
		return date_3_hm_date;
	}

	/**
	 * 得到现在时间
	 * 
	 * @return 字符串 yyyyMMdd HHmmss
	 */
	public static String getStringToday() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 得到现在小时
	 * 
	 * @return
	 */
	public static String getHour() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		String hour;
		hour = dateString.substring(11, 13);
		return hour;
	}

	/**
	 * 得到现在分钟
	 * 
	 * @return
	 */
	public static String getTime() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		String min;
		min = dateString.substring(14, 16);
		return min;
	}

	/**
	 * 转换毫秒数为yyyy-MM-dd HH:mm:ss的字符串
	 * 
	 * @param l
	 *            毫秒格式时间
	 * @return yyyy-MM-dd HH:mm:ss的时间字符串
	 */
	public static String long2DateString(long l) {
		Date d = new Date(l);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(d);
		return dateString;
	}

	/**
	 * 根据用户传入的时间表示格式，返回当前时间的格式 如果是yyyyMMdd，注意字母y不能大写。
	 * 
	 * @param sformat
	 *            yyyyMMddhhmmss
	 * @return
	 */
	public static String getUserDate(String sformat) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(sformat);
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 
	 * @param expire
	 * @return
	 */
	public static boolean compareValidity(Date expire) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(expire);
		Calendar now = Calendar.getInstance();
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.HOUR, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MILLISECOND, 0);
		return calendar.after(now);
	}

	/**
	 * Returns this Date as a millisecond value. The value is the number of
	 * milliseconds since Jan. 1, 1970, midnight GMT.
	 * 
	 * @return
	 */
	public static long getNowMillisecond() {
		Date d = new Date();
		long dl = d.getTime();
		return dl;
	}

	public static final String parseDataString(String code) {// "LastAnswerTime":"\/Date(1304405135317+0800)\/"
		// System.out.println("parseDataString:"+code);
		if (code != null && code.length() > 0) {
			int left = 0;
			// int add = 0;
			int right = 0;
			boolean bAdd = true;
			left = code.indexOf("(");
			if (left >= 0) {
				// add = code.indexOf("+", left);
				// if (add == -1) {
				// add = code.indexOf("-", left);
				// bAdd = false;
				// }
				// // System.out.println("add:"+add);
				// if (add >= 0) {
				right = code.indexOf(")", left);
				// System.out.println("right:"+right);
				if (right > left) {
					long time = Long.parseLong(code.substring(left + 1, right));
					// int atime = Integer.parseInt(code.substring(add + 1,
					// right)) * 36000;
					// time += bAdd ? atime : -atime;
					Date date = new Date();
					date.setTime(time);
					return parseDateString(date.toString());
				}
				// }
			}
		}
		return "";
	}

	static final String[]	months	= { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };

	public static final String parseDateString(String time) {
		if (time != null && time.length() > 0) {
			String[] args = split(time, " ");
			StringBuffer buf = new StringBuffer();
			if (args.length == 6) {
				String month = args[1].toLowerCase();
				for (int i = 0; i < months.length; i++) {
					if (month.equals(months[i])) {
						if (i < 9) {
							month = "0" + String.valueOf(i + 1);
						} else {
							month = String.valueOf(i + 1);
						}
						break;
					}
				}
				buf.append(args[5]).append("-").append(month).append("-").append(args[2]).append(" ").append(args[3].substring(0, 5));
				return buf.toString();
			}
		}
		return "";
	}

	public static final String[] split(String original, String separator) {
		Vector nodes = new Vector();

		// Parse nodes into vector
		int index = original.indexOf(separator);
		while (index >= 0) {
			nodes.addElement(original.substring(0, index));
			original = original.substring(index + separator.length());
			index = original.indexOf(separator);
		}
		// Get the last node
		nodes.addElement(original);

		// Create splitted string array
		String[] result = new String[nodes.size()];
		if (nodes.size() > 0) {
			for (int loop = 0; loop < nodes.size(); loop++) {
				result[loop] = (String) nodes.elementAt(loop);
			}
		}
		return result;
	}

	/**
	 * 判断是否是今天
	 * 
	 * @param lastTime
	 * @return
	 */
	public static boolean isNewDay(long lastTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(lastTime);
		int lastDay = calendar.get(Calendar.DAY_OF_YEAR);
		calendar.setTime(new Date(System.currentTimeMillis()));
		int nowDay = calendar.get(Calendar.DAY_OF_YEAR);
		if (lastDay != nowDay) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否是新的一个月
	 * 
	 * @param lastTime
	 * @return
	 */
	public static boolean isNewMonth(long lastTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(lastTime);
		int lastMonth = calendar.get(Calendar.MONTH);
		calendar.setTime(new Date(System.currentTimeMillis()));
		int nowMonth = calendar.get(Calendar.MONTH);
		if (lastMonth != nowMonth) {
			return true;
		}
		return false;
	}
}
